/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-09-22
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "sys_task.h"
#include "osal_app.h"
#include "app_button.h"
#include "app_pwr_mgr.h"
#include "user_config.h"
#include "bsp_hw_config.h"


#define APP_BTN_LOG_EN  1

#if APP_BTN_LOG_EN
#include "log.h"
#define APP_BTN_LOG     LOG
#else
#define APP_BTN_LOG(...)
#endif

/*----------------------------macro file---------------------------*/
#define BUTTON_DETECT_INT_MS    50
#define RST_BTN_VALID_INT_MS    500

#define BTN_SHORT_PRESS_MAX_CNT 10	 //500ms
#define BTN_LONG_PRESS_CNT      40	 //2s
#define BTN_APP_PAIR_PRESS_CNT  80   //4s
#define BTN_RST_PRESS_CNT       160  //8s

#define DOUBLE_CLICK_VALID_MS   500

#define SYS_SLEEP_EXIT()                  //app_pwr_mgr_exit_sleep
#define SYS_SLEEP_ENTER()                 //app_pwr_mgr_enter_sleep
#define BUTTON_DETECT_TIMER_START()     osal_start_timerEx(osalApp_TaskID, APP_BUTTON_TIMER_EVT, BUTTON_DETECT_INT_MS)

#define BUTTON_CLICK_TIMER_START()      osal_start_timerEx(osalApp_TaskID, APP_BTN_CLICK_TIMEOUT_EVT, 500)

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
static bool btn_is_pressed = false; //Prevent start timer repeatly.
static uint8 btn_press_det_cnt;
static uint32 last_short_pressed_time;
#if(SCHOOL_SKIP_ENABLE)
#define BTN_COUPLE_CLICK_CNT    1  //Press 2 times continuously
#define BTN_SWITCH_CLICK_CNT    4  //Press 5 times continuously
#define BTN_SN_CLICK_CNT        9  //Press 10 times continuously
#define BTN_RST_CLICK_CNT       14 //Press 15 times continuously
static uint8_t couple_click_cnt = 0;
#endif

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

static void app_button_evt_handle(void)
{
    if(false == btn_is_pressed) {
        btn_press_det_cnt = 0;
        SYS_SLEEP_EXIT();
        BUTTON_DETECT_TIMER_START();
        btn_is_pressed = true;
    }
}


int app_button_init(void)
{
    return hal_button_init(app_button_evt_handle);
}


void app_button_click_timeout_handle(void)
{
    last_short_pressed_time = 0;
    couple_click_cnt = 0;
}


void app_button_timer_detect_timeout_handle(void)
{
    if(true == hal_button_press_state_get()) {
        if(btn_press_det_cnt < BTN_RST_PRESS_CNT) {
            btn_press_det_cnt++;
            if(BTN_LONG_PRESS_CNT == btn_press_det_cnt) {
                //sys_task_msg_send(MSG_TYPE_UI, UI_EVT_LONG_PRESS);
            }
			else if(BTN_APP_PAIR_PRESS_CNT == btn_press_det_cnt) {
                //sys_task_msg_send(MSG_TYPE_UI, UI_EVT_APP_PAIR_PRESS);
			}
            BUTTON_DETECT_TIMER_START();
        }
        else {
            //sys_task_msg_send(MSG_TYPE_UI, UI_EVT_SHIP_MODE_RESET);
            btn_is_pressed = false;
        }
    }
    else { //Release
        btn_is_pressed = false;
        if(btn_press_det_cnt <= BTN_SHORT_PRESS_MAX_CNT) {
            uint32 pressed_time = app_get_sys_clock();
            if(pressed_time >= last_short_pressed_time) {
#if(SCHOOL_SKIP_ENABLE)
                if((pressed_time - last_short_pressed_time) <= DOUBLE_CLICK_VALID_MS) {
                    //if(couple_click_cnt <= BTN_COUPLE_CLICK_CNT) {
                    if(couple_click_cnt <= BTN_RST_CLICK_CNT) {
                        couple_click_cnt++;
                        if(couple_click_cnt == BTN_COUPLE_CLICK_CNT) {
                            sys_task_msg_send(MSG_TYPE_UI, UI_EVT_DOUBLE_CLICK);
                        }
                        else if(couple_click_cnt == BTN_SWITCH_CLICK_CNT) {
                            sys_task_msg_send(MSG_TYPE_UI, UI_EVT_LONG_PRESS);
                        }
                        else if(couple_click_cnt == BTN_SN_CLICK_CNT) {
                            sys_task_msg_send(MSG_TYPE_UI, UI_EVT_APP_PAIR_PRESS);
                        }
                        else if(couple_click_cnt == BTN_RST_CLICK_CNT) {
                            sys_task_msg_send(MSG_TYPE_UI, UI_EVT_SHIP_MODE_RESET);
                            last_short_pressed_time = 0;
                        }
                        BUTTON_CLICK_TIMER_START();
                        //last_short_pressed_time = 0;
                        //SYS_SLEEP_ENTER();
                        //return;
                    }
                    else {
                        couple_click_cnt = 0;
                    }
                }
                else {
                    couple_click_cnt = 0;
                    sys_task_msg_send(MSG_TYPE_UI, UI_EVT_SHORT_PRESS);
                }
#else
                if((pressed_time - last_short_pressed_time) <= DOUBLE_CLICK_VALID_MS) {
                    sys_task_msg_send(MSG_TYPE_UI, UI_EVT_DOUBLE_CLICK);
                    last_short_pressed_time = 0;
                    SYS_SLEEP_ENTER();
                    return;
                }
                else {
                    sys_task_msg_send(MSG_TYPE_UI, UI_EVT_SHORT_PRESS);
                }
#endif      //#if(SCHOOL_SKIP_ENABLE)
            }
            else {
                sys_task_msg_send(MSG_TYPE_UI, UI_EVT_SHORT_PRESS);
            }
            last_short_pressed_time = pressed_time;
        }
        SYS_SLEEP_ENTER();
    }
}
